home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Trading on the Edge
/
Trading On The Edge - CD-ROM Toolkit (Wayzata Technology)(2031)(1994).bin
/
pc
/
mac_file
/
vendor_d
/
azte_pro
/
rantok.c
< prev
next >
Wrap
C/C++ Source or Header
|
1993-06-11
|
7KB
|
304 lines
/* program to write out a token file of token values for each player in each
* round of a game, given a 'gametype' that specifies the random number
* parameters for token generation.
* R.G. Palmer 3/90.
*
* Usage:
* Rantok nbuyers nsellers ntokens nrounds gametype
*
* 6/2/91: Modified to allow 5 digit gametypes, specifying ran5. RGP.
*
* 11/92: modified to link with ci.exe and create tokens file distribution
* just like dmon for use with DAD.
*
* modified for use with hi
*/
#define NTRIES 50 /* Previous token.out was 50, so... */
/* various limits as in protocol */
#define MAXTOKENS 4 /* maximum number of tokens each */
#define MAXPLAYERS 20
#define MAXPRICE 8000
/* includes and miscellaneous declarations */
#include "define.h"
#include <sys/types.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#ifndef NO_STDLIB_H
#include <stdlib.h>
#endif
#ifndef RAND_MAX
#define RAND_MAX 2147483647
#endif
extern void error();
static void randset();
static void makerans();
static void maketokens();
static double drand();
static void playertokens();
static int ranrule();
static void equilibrium();
void rantok_main();
/* common abbreviations and macros */
#define RINT register int
#define BLOOP(i) for (i=1; i<=nbuyers; i++)
#define SLOOP(i) for (i=1; i<=nsellers; i++)
/* basic game parameters */
static int nbuyers = 0; /* number of buyers */
static int nsellers = 0; /* number of sellers */
static int gametype = 0; /* type of game */
static int nrounds = 0; /* maximum number of rounds */
static int ntokens = 4; /* number of tokens for each player */
static int ran1 = 0; /* parameter 1 for token choice */
static int ran2 = 0; /* parameter 2 for token choice */
static int ran3 = 0; /* parameter 3 for token choice */
static int ran4 = 0; /* parameter 4 for token choice */
static int ran5 = 0; /* parameter 5 for token choice */
static int ndemand; /* for computing equilibrium. */
static int nsupply; /* " */
static int supply[MAXTOKENS*MAXPLAYERS];
static int demand[MAXTOKENS*MAXPLAYERS];
/* working variables */
static int r = 0; /* round number */
static int buyer[MAXPLAYERS][MAXTOKENS];
static int seller[MAXPLAYERS][MAXTOKENS];
void rantok_main(a,b,c,d,e)
int a,b,c,d,e;
{
FILE *fd;
/* set up random number generator */
randset();
/* get parameters */
nbuyers = d;
nsellers = e;
ntokens = c;
nrounds = b;
gametype = a;
/* make ran1-ran5 */
makerans();
/* Open file tokens.out and write out the sample data (deletes previous info)*/
if ((fd = fopen("tokens.out","w")) == NULL)
error("Can't open tokens.out for writing");
/* Header info for tokens.out */
fprintf(fd,"%d %d %d %d %d\n", gametype, ntokens, nbuyers, nsellers,
NTRIES);
/* make NTRIES sample distributions */
for (r=1; r<=NTRIES; r++)
maketokens(fd);
fclose(fd);
}
static void
maketokens(fd) /* Makes and writes out ntokens for each player */
FILE *fd;
{
RINT i;
double drand();
int offset,boffset,soffset;
int btok[MAXTOKENS],stok[MAXTOKENS];
offset = (int)(ran1*drand());
boffset = offset + (int)(ran2*drand());
soffset = offset;
for (i=0; i<ntokens; i++) {
btok[i] = boffset + (int)(ran3*drand());
stok[i] = soffset + (int)(ran3*drand());
}
BLOOP(i)
playertokens(i,btok,1,fd);
SLOOP(i)
playertokens(i,stok,0,fd);
/* print out the tokens and equilibrium price & trades */
equilibrium(fd);
}
static void
playertokens(player,base,isbuyer,fd)
int player,base[],isbuyer;
FILE *fd;
/* make one player's tokens */
{
RINT j,i;
int tok,toks[MAXTOKENS];
double drand();
int playeroffset;
/* offset for each player, same for all tokens */
playeroffset = (int)(ran5*drand());
/* make tokens */
for (j=0; j<ntokens; j++) {
toks[j] = playeroffset + base[j] + (int)(ran4*drand());
if (toks[j]>MAXPRICE)
toks[j] = MAXPRICE;
}
/* sort into increasing order, straight insertion */
for (j=1; j<ntokens; j++) {
tok = toks[j];
i = j-1;
while (i>=0 && toks[i]>tok) {
toks[i+1] = toks[i];
i--;
}
toks[i+1] = tok;
}
/* copy into player structure in decreasing order if buyer, increasing
* if seller */
if (isbuyer)
for (i=0; i<ntokens; i++)
buyer[player][i] = toks[ntokens-1-i];
else
for (i=0; i<ntokens; i++)
seller[player][i] = toks[i];
/* write out in decreasing order if buyer, increasing if seller */
/* fprintf(fd,"%c %d",(buyer)?'B':'S',r);
for (i=0; i<ntokens; i++)
fprintf(fd," %4d", (isbuyer)? toks[ntokens-1-i]: toks[i]);
fprintf(fd,"\n");
*/
}
static void
makerans() /* calculate ran1-ran4 from gametype */
{
ran5 = ranrule(gametype/10000);
ran1 = ranrule((gametype%10000)/1000);
ran2 = ranrule((gametype%1000)/100);
ran3 = ranrule((gametype%100)/10);
ran4 = ranrule(gametype%10);
}
static int
ranrule(k) /* computes ran(k) = 3**k - 1 */
int k;
{
int i = 1;
if (k > 8) error("illegal digit 9 in gametype");
if (k > 8) k = 8;
while (k--) i *= 3;
return (i-1);
}
static void
randset()
/*
* Set up random number generator
*/
{
time_t time();
double drand();
unsigned int seed;
int i;
seed = (unsigned int) time((time_t *)0);
srand((int)(seed&RAND_MAX));
for (i=0; i<13; i++)
drand(); /* exercise it */
}
static double
drand()
/*
* Random number generator, uniform in 0.0 -- 1.0
*/
{ return ((double) rand())/(((double) RAND_MAX) + 1.0); }
static void equilibrium(fd)
FILE *fd;
{
RINT k,i,j;
int tok,surp;
int eqm,eqmtrades,eqmupper,eqmlower;
int id[MAXTOKENS*MAXPLAYERS];
if (nbuyers==0 || nsellers==0) /* can't happen */
error("in equilibrium");
/* sort all the buyer's tokens into decreasing order */
ndemand = 0;
for (j=0; j< ntokens; j++) {
BLOOP(i) {
tok = buyer[i][j];
k = ndemand-1;
while (k>=0 && demand[k]<tok) {
demand[k+1] = demand[k];
id[k+1] = id[k];
k--;
}
demand[k+1] = tok;
id[k+1] = i;
ndemand++;
}
}
/* sort all the seller's tokens into increasing order */
nsupply = 0;
for (j=0; j<ntokens; j++) {
SLOOP(i) {
tok = seller[i][j];
k = nsupply-1;
while (k>=0 && supply[k]>tok) {
supply[k+1] = supply[k];
id[k+1] = id[k];
k--;
}
supply[k+1] = tok;
id[k+1] = i;
nsupply++;
}
}
/* find the equilibrium #trades, price range, and surplus */
surp = 0;
for (k=0; k<nsupply && k<ndemand && demand[k]>supply[k]; k++)
surp += demand[k]-supply[k];
eqmtrades = k;
if (k > 0) {
eqmupper = demand[k-1];
if (k<nsupply && supply[k]<eqmupper) eqmupper = supply[k];
eqmlower = supply[k-1];
if (k<ndemand && demand[k]>eqmlower) eqmlower = demand[k];
eqm = (eqmlower+eqmupper)/2;
}
else {
eqmlower = MAXPRICE+1;
eqmupper = -1;
eqm = -1;
}
/* Print out a line to the tokens.out file */
for(j=0;j<ndemand;j++)
fprintf(fd," %4d",demand[j]);
for(j=0;j<nsupply;j++)
fprintf(fd," %4d",supply[j]);
fprintf(fd," %4d %4d\n",eqm,eqmtrades);
} /* equilibrium */